/**
 * mail compose javascript
 * @author Phuong Nguyen, <phuong.nguyen@gen.vn> 
 */
function Trim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
function ConertVtoE(str){

	var mang = new Array();
	var strreplace = new Array("A","D","E","I","O","U","Y","a","d","e","i","o","u","y");
	var str_for = str
	mang[0]=new Array("À","Á","Ả","Ã","Ạ","Â","Ấ","Ầ","Ẩ","Ẫ","Ậ","Ắ","Ằ");
	mang[1]=new Array("Đ");
	mang[2]=new Array("È","É","Ẻ","Ẽ","Ẹ","Ê","Ề","Ế","Ể","Ễ","Ệ");
	mang[3]=new Array("Ì","Í","Ỉ","Ĩ","Ị");
	mang[4]=new Array("Ò","Ó","Ỏ","Õ","Ọ","Ô","Ồ","Ố","Ổ","Ỗ","Ộ","Ơ","Ờ","Ớ","Ở","Ỡ","Ợ");
	mang[5]=new Array("Ù","Ú","Ủ","Ũ","Ụ","Ư","Ừ","Ứ","Ử","Ữ","Ự");
	mang[6]=new Array("Ỳ","Ý","Ỷ","Ỹ","Ỵ");
	
	mang[7]=new Array("à","á","ả","ã","ạ","â","ấ","ầ","ẩ","ẫ","ậ","ắ","ằ");
	mang[8]=new Array("đ");
	mang[9]=new Array("è","é","ẻ","ẽ","ẹ","ê","ề","ế","ể","ễ","ệ");
	mang[10]=new Array("ì","í","ỉ","ĩ","ị");
	mang[11]=new Array("ò","ó","ỏ","õ","ọ","ô","ồ","ố","ổ","ỗ","ộ","ơ","ờ","ớ","ở","ỡ","ợ");
	mang[12]=new Array("ù","ú","ủ","ũ","ụ","ư","ừ","ứ","ử","ữ","ự");
	mang[13]=new Array("ỳ","ý","ỷ","ỹ","ỵ");
	
	for (var i=0;i<=mang.length-1;i++)
	{
		for (var i1=0;i1<=mang[i].length-1;i1++)
		{
			str_for=str_for.replace(mang[i][i1],strreplace[i]);
		}
	}

	return str_for;
}
function getKeywords(){
	$("#keyword-c").dialog({
        autoOpen: false,
        width: 650,
        modal: true
    });
	//$("#keyword-c").css("padding", "0px");
	//$("#keyword-c").dialog("open");
	COREJS.v_fShowFromPopup('keyword-c', COREJS._getJsLang('_KEYWORD'), 400, 300);
	$.ajax({
		url : COREJS._getJsLang('_GENCRM_URL')+'/modules/mail/keyword/getKeysForMail',
		data : ({
			to_addresses : document.getElementById("to_addresses").value,
		}),
		type : 'post',
		beforeSend : function(){
			
		},
		success : function(response){
			jQuery("#keywords").html(response);
		}
	})
}
/**
 * Personalize email sent by keword
 * @author VU Anh Tuan <tuan.vu@gen.vn>
 * @since 2012-05-14 11:06
 */
function VgetKeywords(){
	$("#keyword-c").dialog({
        autoOpen: false,
        width: 650,
        modal: true
    });
	//$("#keyword-c").css("padding", "0px");
	//$("#keyword-c").dialog("open");
	COREJS.v_fShowFromPopup('keyword-c', COREJS._getJsLang('_KEYWORD'), 400, 300);
	$.ajax({
		url : COREJS._getJsLang('_GENCRM_URL')+'/modules/mail/keyword/VgetKeysForMail',
		data : ({
			to_addresses : document.getElementById("to_addresses").value,
			Tieu_De 	 : document.getElementById('Tieu_De').value,
			elm1 		 : $('#elm1').html()
		}),
		type : 'post',
		beforeSend : function(){
			
		},
		success : function(response){
			jQuery("#keywords").html(response);
		}
	})
}

/**
 * Preview email before sending
 * @author VU Anh Tuan <tuan.vu@gen.vn>
 * @since 2012-05-14 18:08
 */
function previewMail(){
	//$("#keyword-c").css("padding", "0px");
	//$("#keyword-c").dialog("open");
	COREJS.v_fShowFromPopup('keyword-c', COREJS._getJsLang('_MAIL_BTN_PREVIEW'), 650, 300);
	$.ajax({
		url : COREJS._getJsLang('_GENCRM_URL')+'/modules/mail/compose/VpreviewMail',
		/*
		data : ({
			to_addresses : document.getElementById("to_addresses").value,
			Tieu_De 	 : document.getElementById('Tieu_De').value,
			elm1 		 : $('#elm1').html()
		}),
		*/
		type: 'POST',
		data: $("#CmailForm").serialize(),
		dataType: 'json',
		beforeSend : function(){
			
		},
		success : function(previewResponse){
			jQuery("#ui-dialog-title-keyword-c").html(previewResponse.TieuDe);
			jQuery("#keywords").html(previewResponse.NoiDung);
		}
	})
	$("#keyword-c").dialog({
        autoOpen: false,
        width: 650,
        position: "top",
        modal: true
    });
}

function getmailTemp(){
	COREJS.v_fShowInpagePopup('mail-tmp-list', COREJS._getJsLang('_SELECT_TEMPLATE'), 600, 300,
			function(){},
			function(){$('#mail-tmp-list').dialog("close");}
		);
	Template.getTemplates(1);
	
	url = COREJS._getBaseUrl() + '/modules/mail/sync/isSynchronized';
	COREJS.v_fGetJson(url, '', function(response){
		if (response.sync == 1) {
			COREJS.v_fShowOKDialog(COREJS._getJsLang('_MAIL_TEMPLATE_NEED_SYNC'), function(){
				s_url = url = COREJS._getBaseUrl() + '/modules/mail/sync/sync';
				COREJS.v_fGetJson(url, '', function(res){
					COREJS.v_fShowOKDialog(COREJS._getJsLang('_MAIL_TEMPLATE_SYNC_DONE'), function(){
						$('#mail-tmp-list').dialog("close");
						Template.getTemplates(1);
					});
				});
			});
		}
	});
}
function closePopup(id){
	$("#"+id).dialog("close");
}
function setTemplate(id){
	var body = document.getElementById("t"+id).innerHTML;
	$("#template-c").dialog("close");
	$("#editortn").html('<textarea id="elm1" name="elm1" rows="25" cols="80" style="width: 100%" class="tinymce1">'+body+'</textarea>');
	a("tinymce1");
	$('#mail-tmp-list').dialog("close");
}
var Suggestion = {
		search : function(key1,types){
			url = 'index.php?con=compose/searchsugg';
			params = {
					keyword1: key1,
					type:	types,
			}
			$.post(url,params, function(response){
				if(response.results!=''){
					var html = "";
					var ar = new Array();
					var numb = 0;
					key1 = Trim(key1);
					for(i=0; i<response.results.length; i++){
						if(i==0){
							hover = "hover";
						}else{
							hover = "";
						}
						to_c = response.results[i];
						
						//test = ConertVtoE(response.results[i]);
						test = ConertVtoE(to_c);
						to_scratch = test.replace("<","&lt;");
						
						to_replace = to_scratch.toLowerCase();
						numb = to_replace.indexOf(key1.toLowerCase());
						
						//numb = getCharFirst(key1.toLowerCase(),response.results[i].toLowerCase());
						subtext = to_scratch.substring(numb,key1.length+numb);
						//text = text.replace("<","&lt;")
						text 	= to_scratch.replace(subtext,"<span class='s-b-t'>"+subtext+"</span>");
						
						html +="<div onclick='getVal(\""+key1+"\",\""+types+"\");' onmouseover='goover(\""+types+"_"+i+"\");' id='"+types+"_"+i+"' rel='"+to_c+"' class='item-m-sg "+hover+"'>"+text+"</div>";
					}
					document.getElementById("ss-"+types+"-add-s").innerHTML = html;
					
					//document.getElementById("ss-"+types+"-add-s").innerHTML = response.results;
					altoggle('ss-'+types+'-add');
				}else{
					$("#ss-"+types+"-add").css("display","none");
				}
			},'json')
		}
}
function goover(div){
	var id = $(".hover").attr("id");
	$("#"+id).removeClass("hover");
	$("#"+div).addClass("hover");
}
function getVal(key,types){
	mail_o = $("#"+types+"_addresses").val().replace(key,"");
	$("#"+types+"_addresses").attr('value',mail_o+' '+$("#ss-"+types+"-add-s .hover").attr("rel")+', ');
	closeSuggPop();
}
function closeSuggPop(){
	$(".s-mail-sg").css("display","none");
	$(".s-mail-res").html("");
}
function SearchSugges(event,types){
	var charCode = (event.which)?event.which:event.keyCode
	var key = $("#"+types+"_addresses").val();
	if(key.indexOf(',')!=-1){
		var key_arr = key.split(',');
		key = key_arr[key_arr.length-1];
	}
	if(charCode!=32 && charCode!=37 && charCode!=38 && charCode!=39 && charCode!=40 && charCode!=13){
		Suggestion.search(key,types);
	}else{
		var i=0;
		$("#ss-"+types+"-add .item-m-sg").each(function() {
				i++;
			});
		if(charCode==38){
			var step = $(".hover").attr("id");
			step = step.replace(types+"_","");
			if(step!=0){
				var next = step*1-1;
				$("#"+types+"_"+step).removeClass("hover");
				$("#"+types+"_"+next).addClass("hover");
			}
			return false;
		}
		if(charCode==40){
			var step = $(".hover").attr("id");
			step = step.replace(types+"_","");
			if(step<i-1){
				var next = step*1+1;
				$("#"+types+"_"+step).removeClass("hover");
				$("#"+types+"_"+next).addClass("hover");
			}
			return false;
		}
		if(charCode==13){
			if(i>0){
				getVal(key,types);
				closeSuggPop();
			}
			return false;
		}
		return false;
	}
}
var Drafts = {
	load : function(id){
		url = 'index.php?con=draft/getObject&d='+id;
		$.getJSON(url, null,
				function(response){
					$("#editortn").html('<textarea id="elm1" name="elm1" rows="25" cols="80" style="width: 100%" class="tinymce1">'+response.body+'</textarea>');
					a("tinymce1");
				document.getElementById('isnew').value = response.id;
				if(response.file){
					jQuery("#link_upload").remove();
					jQuery("#file_dk").html(response.file);
				}
				if(response.link_attach)
					jQuery("#fileadddk").html(response.link_attach);
				if(response.subject)
					jQuery("#Tieu_De").attr("value",response.subject);
				if(response.to)
					jQuery("#to_addresses").html(response.to);
				if(response.to_cc){
					jQuery("#cc_addresses").html(response.to_cc);
					addCC();
				}
				if(response.to_bcc){
					jQuery("#bb_addresses").html(response.to_bcc);
					addBCC();
				}
				if(response.time_draft){
					jQuery("#timedrafts").html(response.time_draft);
				}
			});
		},
	save : function(isnew, typeRq){
		var to_addresses 	= document.getElementById("to_addresses").value;
		var cc_addresses	= document.getElementById("cc_addresses").value;
		var bb_addresses	= document.getElementById("bb_addresses").value;
		var Tieu_De		= document.getElementById("Tieu_De").value;
		var elm1			= $('#elm1').html();
		if($("input[name='attachSys[]']")){
			var attachSys = $("input[name='attachSys[]']").serialize();
		}else{
			var attachSys = '';
		}
		if(to_addresses!='' || cc_addresses!='' || bb_addresses!='' || Tieu_De!=''){
			$.ajax({
				url : 'index.php?con=draft/saveDraft',
				data : ({
						to_addresses: to_addresses,
						cc_addresses: cc_addresses,
						bb_addresses: bb_addresses,
						Tieu_De:	Tieu_De,
						elm1:		elm1,
						isnew: isnew,
						attachSys : attachSys,
						typeRq : typeRq
					    //data: $("#CmailForm").serialize(),
					  }),
				type: 'POST',
				beforeSend : function(){
					COREJS.AlertMsg(COREJS._getJsLang('_DANG_LUU'));
				},
				dataType: 'json',
				success : function (response){
					COREJS.CloseMsg();
					window.parent.location.hash = 'drafts/'+response.draft;
					document.getElementById('isnew').value = response.draft;
					$("#timedrafts").html(response.msg);
				}
			})
		}
	}
}
function addCC(){
	jQuery("#inv_cc").hide();
	jQuery("#to_cc").show();
	jQuery("#cc_addresses").focus();
}
function addBCC(){
	jQuery("#inv_bcc").hide();
	jQuery("#to_bcc").show();
	jQuery("#bb_addresses").focus();
}

function attachfile(){
	var i=0;
	jQuery("div.item-attch").each(function(){
		i++;
	})
	var anchor = getAnchor();
	if(anchor){
		draft = '&draft='+anchor[1];
	}else{
		draft = '';
	}
	html = "<div class=\"item-attch\" style='clear: both' id='input"+i+"'><input type=\"file\" name=\"Duong_Dan"+i+"\" onchange=\"fileUpload(this.form,'index.php?con=compose/upload&f="+i+draft+"','input"+i+"');\" />";
	html +=" <a href=\"javascript:void(0)\" onclick='rminput(\"input"+i+"\")'>"+COREJS._getJsLang('_DELETE')+"</a></div>";
	
	html2 ="<div><a href=\"javascript:void(0)\" onclick=\"attachfile()\">"+COREJS._getJsLang('_ATTACH_OTHER_FILE')+"</a></div>"
	jQuery("#file_dk").append(html);
	if(i==0){
		jQuery("#fileadddk").append(html2);
		jQuery("#link_upload").css("display","none");
	}
}
function rminput(wr){
	jQuery("#"+wr).remove();
	var i=0;
	jQuery("div.item-attch").each(function(){
		i++;
	})
	if(i==0){
		jQuery("#link_upload").css("display","block");
		jQuery("#fileadddk").html('');
	}
}
	/**
	*edited by phuong.nguyen
	*/
function checkFromatMail(str,field){
	var ok = true;
	$.ajax({
		url : 'index.php?con=compose/CheckFormatMailBeforeSend',
		data : ({
				to_addresses: document.getElementById("to_addresses").value,
				field: field,
			  }),
		type: 'POST',
		beforeSend : function(){
		},
		dataType: 'json',
		async: false,
		success : function (response){
			if(response.mail_err!=''){
				alert(COREJS._getJsLang('_MAIL_ERROR_FORMAT')+response.mail_err+COREJS._getJsLang('_MAIL_ERROR_FORMAT2')+response.field+"\n"+COREJS._getJsLang('_MAIL_ERROR_FORMAT3'));
				ok=false;
			}
		}
	})
	return ok;
}
function checkformmail(){
	var mailto = document.getElementById("to_addresses");
	var tieude = document.getElementById("Tieu_De");
	if(mailto.value==''){
		alert(COREJS._getJsLang('_MAILTO_NOT_NULL'));
		mailto.focus();
		return false;
	}/*else{
		if(checkFromatMail(mailto,'to_addresses')==false){
			return false;
		}
	}*/
	if(tieude.value==''){
		alert(COREJS._getJsLang('_TITLE_NOT_NULL'));
		tieude.focus();
		return false;
	}
	return true;
}
function sendmail(){
	if(checkformmail()){
		if($("input[name='attach[]']")){
			var attach = $("input[name='attach[]']").serialize();
		}else{
			var attach = '';
		}
		if($("input[name='attachSys[]']")){
			var attachSys = $("input[name='attachSys[]']").serialize();
		}else{
			var attachSys = '';
		}
		if($("input[name='aaID[]']")){
			var aaID = $("input[name='aaID[]']").serialize();
		}else{
			var aaID = '';
		}
		if($('#moduel').val()==5)
		{
			createworkemail($('#idkh').val());
		}

		$.ajax({
			/*url: 'index.php?con=compose/send',*/
			url: COREJS._getJsLang('GENCRM_URL') + '/modules/mail/compose/send',
			/*
			data: ({
				idac        	: $('#sendfrom').val(),
				elm1 			: $('#elm1').html(),
				Tieu_De 		: document.getElementById('Tieu_De').value,
				to_addresses 	: document.getElementById('to_addresses').value,
				cc_addresses 	: document.getElementById('cc_addresses').value,
				bb_addresses 	: document.getElementById('bb_addresses').value,
				attach			: attach,
				attachSys		: attachSys,
				aaID			: aaID,
				isnew			: document.getElementById('isnew').value,
				inTurn			: document.getElementById('inTurn').value
			}),
			*/
			data: $("form").serialize(),
			type: "POST",
			dataType: 'json',
			beforeSend: function(){
				/*document.getElementById("autosave").value=0;*/
				COREJS.AlertMsg(COREJS._getJsLang('_SENDING'));
			},
			success: function(reponseMsg){
				if(reponseMsg.error)
					{
						alert(reponseMsg.msg);
						$(".msg-sys-mail").text(reponseMsg.msg)
						$(".msg-sys-mail").show();
						//COREJS.v_fShowOKDialog(reponse.error);
						
					}else{
						//window.location.replace(COREJS._getJsLang('_GENCRM_URL')+'/modules/mail/compose');
						window.location.href = COREJS._getJsLang('_GENCRM_URL') + '/modules/mail/stats';
					}
			}
		})
	}
}

function createworkemail(idkh)
{
	var urlaction = COREJS._getJsLang('GENCRM_URL') + '/modules/marketing/index/creaworkmail?idkh='+idkh+'&idmk='+$('#idmk').val();
	$.getJSON(urlaction);
}
function saveDrafts(type){
	var isnew = document.getElementById('isnew').value;
	Drafts.save(isnew, type);
	//alert('Draft debug!');
	
	// update count of draft on left-sidebar
	url = COREJS._getBaseUrl() + '/modules/mail/draft/count';
	COREJS.v_fGetJson(url, '', function(response){
		$('#count-draft-lf').html(response.n);
	});	
}
function cancelmail(){
	if(anchor[0]=='drafts'){
		window.location.href = COREJS._getJsLang('_GENCRM_URL')+'/modules/mail/draft/draftdestroy&id='+anchor[1];
	}else{
		window.location.href = COREJS._getJsLang('_GENCRM_URL')+'/modules/mail/compose/cancel';
	}
}
function fileUpload(form, action_url, div_id)
{
// Create the iframe...
var iframe = document.createElement("iframe");
iframe.setAttribute("id","upload_iframe");
iframe.setAttribute("name","upload_iframe");
iframe.setAttribute("width","0");
iframe.setAttribute("height","0");
iframe.setAttribute("border","0");
iframe.setAttribute("style","width: 0; height: 0; border: none;");
 
// Add to document...
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name="upload_iframe";
 
iframeId = document.getElementById("upload_iframe");
 
// Add event...
var eventHandler = function()  {
 
if (iframeId.detachEvent)
iframeId.detachEvent("onload", eventHandler);
else
iframeId.removeEventListener("load", eventHandler, false);
 
// Message from server...
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
}
 
if (iframeId.addEventListener)
iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent)
iframeId.attachEvent("onload", eventHandler);
 
// Set properties of form...
form.setAttribute("target","upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method","post");
form.setAttribute("enctype","multipart/form-data");
form.setAttribute("encoding","multipart/form-data");
 
// Submit the form...
form.submit();
document.getElementById(div_id).innerHTML = "Uploading...";
}
function getAnchor(){
	var addr = (String)(window.location);
	if(addr.indexOf('#')!=-1){
		var arexpl = addr.split('#');
		if(arexpl[1].indexOf('/')!=-1){
			var param = arexpl[1].split('/');
			return param;
		}
	}
	return false;
}
function changeC(chk,id){
	if(chk==false){
		$("#chk"+id).attr("name","aa[]");
		$("#chkID"+id).attr("name","aaID[]");
	}else{
		$("#chk"+id).attr("name","attach[]");
		$("#chkID"+id).attr("name","attachID[]");
	}
}
var anchor = getAnchor();
if(anchor[0]=='drafts'){
	Drafts.load(anchor[1]);
}else{
	a("tinymce");
}

/**
 * MAIL_COMPOSE
 * 
 * @author		Tran Anh Tuan <tuan.tran@gen.vn>
 * @category	Mail
 * @package		scripts
 * @copyright	2011 Gen Technology
 */
var MAIL_COMPOSE = {
	/**
	 * Show tooltip
	 */
	showTip : function(content){
		Tip('<img src="'+ COREJS._getBaseUrl()+'/modules/mail/resources/email_templates/'+content+'" width="247" height="200" />', SHADOW, true, SHADOWCOLOR, '#ffbbcc', BORDERCOLOR, '#555555', BGCOLOR, '#ffffe1', 247, 200);
	},
	
	getSignature:function(id){
		var urlaction = COREJS._getBaseUrl()+'/modules/mail/compose/getSignature?idac='+id;
		$.getJSON(urlaction,null,function(data){
			var html = $("#elm1").html();
			html= html.toString();
			var index = html.indexOf('<div id="Signature"');
			html= html.substring(0,index);
			if (data) {
				$("#editortn").html('<textarea id="elm1" name="elm1" rows="25" cols="80" style="width: 100%" class="tinymce1">' + html + '<div id="Signature" style="text-align: left; ">--<br />' + data + '</div></textarea>');
			}else
			{
			$("#editortn").html('<textarea id="elm1" name="elm1" rows="25" cols="80" style="width: 100%" class="tinymce1">' + html + '<div id="Signature" style="text-align: left; ">&nbsp;</div></textarea>');
			}	
			a("tinymce1");
		});
	},
	
	/**
	 * Show templates popup
	 * 
	 * @author		Tran Anh Tuan <tuan.tran@gen.vn>
	 * @param		none
	 * @return		void
	 */
	v_fGetTemplates : function()
	{
		COREJS.v_fShowInpagePopup('tmp-select', COREJS._getJsLang('_SELECT_TEMPLATE'), 600, 490,
				function(){
				},
				function(){
					$('#tmp-select').dialog("close");
				}
		);
		url_check_sync = COREJS._getBaseUrl() + '/modules/mail/template/isneedsync';
		COREJS.v_fGetJson(url_check_sync, null, function(response_sync){
			if (0 == response_sync.e)
			{
				if (0 == response_sync.s)
				{
					url = COREJS._getBaseUrl() + '/modules/mail/template/loadTemplates';
					COREJS.v_fGetJson(url, null, function(response){
						if (0 == response.e)
						{
							$('#tmp-listivew').html(response.listview);
							$('#tmp-paginator').html(response.paginator)
						}
						else
						{
							// Show error dialog
							COREJS.v_fShowOKDialog(response.e);
						}
					});					
				}
				else
				{
					// Need synchronize
					url_sync_process = COREJS._getBaseUrl() + '/modules/mail/template/synchronize';
					COREJS.v_fGetJson(url_sync_process, null, function(response_process){
						if (0 == response_process.e)
						{
							COREJS.v_fShowOKDialog('Đã đồng bộ <b> '+response_process.n+'</b> thư mẫu', function(){
								// Get templates
								url = COREJS._getBaseUrl() + '/modules/mail/template/loadTemplates';
								COREJS.v_fGetJson(url, null, function(response){
									if (0 == response.e)
									{
										$('#tmp-listivew').html(response.listview);
										$('#tmp-paginator').html(response.paginator)
									}
									else
									{
										// Show error dialog
										COREJS.v_fShowOKDialog(response.e);
									}
								});								
								
							});
						}	
						else
						{
							// Process error
							COREJS.v_fShowOKDialog(response_process.e);
						}	
					});
				}	
			}
			else 
			{
				// Error sync
				COREJS.v_fShowOKDialog(response_sync.e);
			}
		});
			
	},
	
	/**
	 * Get templates by page
	 * 
	 * @author		Tran Anh Tuan <tuan.tran@gen.vn>
	 * @param		integer
	 * @return		void
	 */
	v_fGetTemplatesPage : function(the_iPage)
	{
		url = COREJS._getBaseUrl() + '/modules/mail/template/loadTemplates/?page='+the_iPage;
		COREJS.v_fGetJson(url, null, function(response){
			if (0 == response.e)
			{
				$('#tmp-listivew').html(response.listview);
				$('#tmp-paginator').html(response.paginator)
			}
			else
			{
				// Show error dialog
				COREJS.v_fShowOKDialog(response.e);
			}
		});		
	},
	
	/**
	 * Set selected template for Compose editor
	 * 
	 * @author		Tran Anh Tuan <tuan.tran@gen.vn>
	 * @param		none
	 * @return		void
	 */
	v_fSetTemplate : function(the_TemplateId)
	{
		url = COREJS._getBaseUrl() + '/modules/mail/template/getTemplate/?tmp=' + the_TemplateId;
		COREJS.v_fGetJson(url, null, function(response){
			if (0 == response.e) {
				tinymce.execCommand('selectall');
				tinymce.execCommand('delete');
				tinymce.execCommand('mceInsertContent', false, response.html);
				$('#tmp-select').dialog("close");
			}
			else {
				COREJS.v_fShowOKDialog(response.e);
			}
		});
	}
}
